package com.dcbeida.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.addnewpatient.AddNewPatientAct;
import com.dcbeida.adapter.MyClientAdapter;
import com.dcbeida.bean.MyClientBean;
import com.dcbeida.mine.detail.MyClientDetailAct;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 我的客户
 * Created by 150410 on 2017/9/18.
 */

public class MyClientActivity extends BaseActivity implements XListView.IXListViewListener, MineContract.View, AdapterView.OnItemClickListener {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.myList)
    XListView myList;
    @Bind(R.id.ll_no_data)
    LinearLayout llNoData;
    @Bind(R.id.ll_header1)
    LinearLayout ll_header1;
    @Bind(R.id.ll_header2)
    LinearLayout ll_header2;
    @Bind(R.id.input_search)
    EditText inputSearch;
    @Bind(R.id.et_query)
    EditText etQuery;
    @Bind(R.id.img_back2)
    ImageView imgBack2;
    @Bind(R.id.tv_search)
    TextView tvSearch;

    private List<Object> datalist;
    private MyClientAdapter adapter;
    private MinePersenter mPresent;

    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 10;

    int type = DataLoadDirection.Refresh;
    private String str;

    @Override
    protected int getLayoutResId() {
        return R.layout.my_client;
    }

    @Override
    public void setData() {
        ll_header1.setVisibility(View.VISIBLE);
        ll_header2.setVisibility(View.GONE);

        mPresent = new MinePersenter(this, this);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String search = bundle.getString("search");
            if (!TextUtils.isEmpty(search)) {
                ll_header1.setVisibility(View.GONE);
                ll_header2.setVisibility(View.VISIBLE);
            }
        } else {
            mPresent.getList(pageindex, pagesize, str, "", "0", "");
        }
        imgBack.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.kehu);
        tvRight.setText(R.string.add);
        etQuery.setHint("输入患者姓名或者手机号");

        datalist = new ArrayList<Object>();
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        myList.setOnItemClickListener(this);
        adapter = new MyClientAdapter(this);
        myList.setAdapter(adapter);

        etQuery.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE){
                    Log.i("fancl","点击了回车键");
                    str = etQuery.getText().toString().trim();
                    mPresent.getList(0, pagesize, str, "", "0", "");
                }else if(actionId ==EditorInfo.IME_ACTION_NEXT){
                    Log.i("fancl","点击了下一步");
                    str = etQuery.getText().toString().trim();
                    mPresent.getList(0, pagesize, str, "", "0", "");
                }else if(actionId ==EditorInfo.IME_ACTION_SEND){
                    Log.i("fancl","点击了发送");
                    str = etQuery.getText().toString().trim();
                    mPresent.getList(0, pagesize, str, "", "0", "");
                }


                return false;
            }
        });

    }

    @OnClick({R.id.img_back, R.id.img_back2, R.id.tv_right, R.id.tv_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
            case R.id.img_back2:
                finish();
                break;
            case R.id.tv_right:
                showActivity(AddNewPatientAct.class, null);
                break;
            case R.id.tv_search:
                if (TextUtils.isEmpty(inputSearch.getText().toString().trim())) {
                    showTip(getString(R.string.qsrhzxm));
                    return;
                }

                str = inputSearch.getText().toString().trim();
                mPresent.getList(0, pagesize, str, "", "0", "");
                break;
        }
    }

    @Override
    public void onRefresh() {
        // 执行刷新任务
        type = DataLoadDirection.Refresh;
        mPresent.getList(0, pagesize, str, "", "0", "");
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务
        type = DataLoadDirection.LoadMore;
        if (isEnd) {
            mPresent.getList(pageindex + 1, pagesize, str, "", "0", "");
        }
    }



    @Override
    public void setPresenter(MineContract.Persent presenter) {

    }

    @Override
    public void showTelTip(String msg) {
        showTip(msg);
    }

    @Override
    public void showView(boolean hasData) {
        if (hasData) {
            myList.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);
        } else {
            myList.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showList(ArrayList<MyClientBean> list) {
        if (list.size() < 10)
            isEnd = false;
        else
            isEnd = true;

        Totle = adapter.getmArrayList().size() + list.size();


        if (type == DataLoadDirection.Refresh) {
            myList.stopRefresh();
            pageindex = 0;
            adapter.clearList();
        } else if (type == DataLoadDirection.LoadMore) {
            myList.stopLoadMore();
            pageindex++;
        }

        if (isEnd) {
            myList.showFooterView();
            myList.setPullLoadEnable(true);
        } else {
            myList.hideFooterView();
            myList.setPullLoadEnable(false);
        }

        adapter.setmArrayList(list);

        if (adapter.getCount()>0) {
            myList.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);
        } else {
            myList.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        MyClientBean bean = (MyClientBean) adapter.getItem(position - 1);
        bundle.putSerializable("myClientBean", bean);
        bundle.putInt("operaterType",1);
        showActivity(MyClientDetailAct.class, bundle);
    }

}
